import QtQuick 1.1
import com.nokia.symbian 1.1
import QtMultimediaKit 1.1
import "core" 1.0
import "core/lines.js" as Logic
import "core/Constants.js" as Constant

MainPage {
    //anchors.fill: parent
    id: gamePageC
    property int score: 0
    property int balls: 0
    property int margin: 10
    property bool saveGame: false
    property bool restartGame: false
    property bool continueGame: false

    property bool dataAvailable: false

    onRestartGameChanged: {
        if (restartGame){
            if (Logic.isDataAvailable()){
                //TODO: Display confirm dialog
            }
            //TODO: if answer YES!!
            Logic.startNewGame();
            Logic.deleteSavedGame();
        }
        restartGame = false;
    }

    onContinueGameChanged: {
        if (continueGame){
            if (Logic.isDataAvailable()){
                Logic.loadSavedGame();
            }
        }
        continueGame = false;
    }
    onSaveGameChanged: {
        if (saveGame){
            Logic.saveGame();
        }
        saveGame = false;
    }

    Row{
        id: controlBar
        opacity: 1
        visible: true
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.left: parent.left
        Text{
            id: scoreText
            width: gamePageC.width/2
            height: 50
            horizontalAlignment:Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: "Score " + score
            color: "white"
        }

        Text{
            id: ballText
            width: gamePageC.width/2
            height: 50
            horizontalAlignment:Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: "Balls " + balls
            color: "white"
        }

    }

    MouseArea{
        height: gameGrid.height
        width: gameGrid.width
        anchors {verticalCenter: parent.verticalCenter; horizontalCenter: parent.horizontalCenter}

        Grid{
            id: gameGrid

            property int blockSize: Constant.tileSize
            property int numClicked: 0
            columns: 9
            spacing: 0

            Repeater{
                id: repeatTile
                model: 81
                delegate: Tile{ }
            }
        }
        onClicked: {
            console.debug(mouse.x);
            Logic.handleClick(mouse.x,mouse.y);
        }
    }

    SoundEffect {
        id: bombSnd
        source: "sound/explode.wav"
        volume:soundVolume
    }

    SoundEffect {
        id: moveSnd
        source: "sound/close.wav"
        volume:soundVolume
    }
    Dialog {
        id: nameInputDialog

        property int initialWidth: 0
        property alias name: nameInputText.text
        height: dialogText.height+nameInputText.height+50;
        anchors.centerIn: parent
        z: 22;
        width: dialogText.width;

        Behavior on width {
            NumberAnimation {}
            enabled: nameInputDialog.initialWidth != 0
        }

        onClosed: {
            if (nameInputText.text != "")
                Logic.saveHighScore(nameInputText.text);
            Logic.startNewGame();
        }
        Text {
            id: dialogText
            anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }
            text: "Game over! Please enter your name: "
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (nameInputText.text == "")
                    nameInputText.openSoftwareInputPanel();
                else
                    nameInputDialog.forceClose();
            }
        }

        TextInput {
            id: nameInputText
            anchors { horizontalCenter: parent.horizontalCenter; top: dialogText.bottom }
            focus: visible
            autoScroll: false
            maximumLength: 24
            onAccepted: {
                nameInputDialog.forceClose();
            }
        }
    }
}
